﻿Ext.define('app.system.system_role_auth.ProjectAuth', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [me.getRoleGrid(), me.getPermissionGrid()];
        me.callParent();
    },
    getRoleGrid: function () {
        var me = this;
        if (!this.roleGrid) {
            this.roleGrid = new Ext.grid.Panel({
                title: '角色列表',
                region: 'west',
                split: true,
                width: 300,
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetRoles', rootProperty: 'result.items' }),
                columns: [
                    { header: "角色", dataIndex: 'name' },
                    { header: "显示名", dataIndex: 'displayName' },
                    {
                        header: "创建时间", dataIndex: 'creationTime', renderer: function (value, p, record) {
                            return Ext.util.Format.date(value, 'Y-m-d');
                        }
                    }
                ],
                listeners: {
                    select: function (grid, record, index, eOpts) {
                        me.getPermissionGrid().store.load({ params: { roleId: record.data.id } });
                    }
                }
            });
        }
        return this.roleGrid;
    },
    getPermissionGrid: function () {
        var me = this;
        if (!this.permissionGrid) {
            var store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetProjectPermission', autoLoad: false });
            store.load({ params: { roleId: 0 } });
            this.permissionGrid = new Ext.grid.Panel({
                title: '权限列表',
                region: 'center',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                tbar: [
                    { text: '保存', icon: '/Content/images/page_save.png', handler: function () { me.save(); } },
                    "-",
                    {
                        text: '全选', icon: '/Content/images/accept.png', handler: function () {
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                record.data.hasAuth = true;
                                record.commit();
                            }
                        }
                    },
                    "-",
                    {
                        text: '全不选', icon: '/Content/images/cancel.png', handler: function () {
                            for (var i = 0; i < store.getCount() ; i++) {
                                var record = store.getAt(i);
                                record.data.hasAuth = false;
                                record.commit();
                            }
                        }
                    },
                    "-",
                    "可见范围：",
                    {
                        xtype: 'combo',
                        editable: false,
                        store: ['null', 'all', 'business', 'center', 'room'],
                        listeners: {
                            select: function (combo, record, eOpts) {
                                var value = combo.getValue();
                                for (var i = 0; i < store.getCount() ; i++) {
                                    var record = store.getAt(i);
                                    record.data.scope = value;
                                    record.commit();
                                }
                            }
                        }
                    }
                ],
                plugins: {
                    ptype: 'cellediting',
                    clicksToEdit: 1
                },
                store: store,
                columns: [
                    { header: "权限项", sorttable: false, dataIndex: 'permission' },
                    { xtype: 'checkcolumn', sorttable: false, header: "是否授予", dataIndex: 'hasAuth' },
                    {
                        header: "可见范围", sorttable: false, dataIndex: 'scope', editor: {
                            xtype: 'combo',
                            editable: false,
                            store: ['null', 'all', 'business', 'center', 'room']
                        }
                    }
                ]
            });
        }
        return this.permissionGrid;
    },
    save: function () {
        var me = this;
        var sel = me.getRoleGrid().getSelection();
        if (sel.length == 0) {
            Ext.Msg.alert('系统提示', '请选择授权角色');
            return false;
        }
        var roleId = sel[0].data.id;

        var store = me.getPermissionGrid().store;
        var arry = [];
        for (var i = 0; i < store.getCount() ; i++) {
            var record = store.getAt(i);
            arry.push({
                Permission: record.data.permission,
                HasAuth: record.data.hasAuth,
                Scope: record.data.scope
            });
        }


        App.AjaxPost('/api/services/app/role/SetProjectPermission', { RoleId: roleId, Permissions: arry }, function (data) {
            me.getPermissionGrid().store.load({ params: { roleId: roleId } });
        });


    }
});